﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;

namespace BaccaratRally.DataKernel.Service
{
    using BaccaratRally.DataKernel.SqlGateway;
    using BaccaratRally.DataKernel.Entity;

    public class UserService : ServiceBase
    {
        public virtual User Create(User newItem)
        {
            return SqlGateway.CreateInstance().Insert<User>(newItem);
        }

        public virtual User Update(User item)
        {
            return SqlGateway.CreateInstance().Update<User>(item);
        }

        public virtual List<User> GetUserList()
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("SELECT * FROM User");

            return SqlGateway.CreateInstance().Select<User>(sqlstring.ToString(), CommandType.Text).ToList();
        }

        public virtual User GetUser(string userName)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("SELECT * FROM dbo.[User] where UserName = '" + userName + "' ");

            return SqlGateway.CreateInstance().SelectSingle<User>(sqlstring.ToString(), CommandType.Text);
        }

        public virtual User GetUser(string userName, string password)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("SELECT * FROM dbo.[User] where UserName = '" + userName + "' and Password = '" + password + "'");

            return SqlGateway.CreateInstance().SelectSingle<User>(sqlstring.ToString(), CommandType.Text);
        }

        public virtual void UpdateSetting(string userName, string setting)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("UPDATE [User] Set UserSetting = '" + setting + "' WHERE UserName = '" + userName + "'");

            SqlGateway.CreateInstance().ExecuteNonQuery(sqlstring.ToString(), CommandType.Text);
        }
    }
}
